package com.fitzone.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitzone.model.dto.ActivityDTO;
import com.fitzone.model.dto.ActivityPageDTO;
import com.fitzone.model.entity.Activity;
import com.fitzone.web.mapper.ActivityMapper;
import com.fitzone.web.service.ActivityService;
import net.minidev.json.JSONUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private RestHighLevelClient client;
    @Override
    public void saveActivity(ActivityDTO activityDTO) {
        Activity activity = new Activity();
        activity.setStatus(1);
        activity.setCover(activityDTO.getCover());
        activity.setUrl(activityDTO.getUrl());
        activity.setTypes(activityDTO.getTypes());
        this.save(activity);
    }

    @Override
    public void updateActivity(ActivityDTO activityDTO) {
        Activity activity = new Activity();
        activity.setStatus(1);
        activity.setId(activityDTO.getId());
        activity.setCover(activityDTO.getCover());
        activity.setUrl(activityDTO.getUrl());
        activity.setTypes(activityDTO.getTypes());
        this.updateById(activity);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Activity activity = new Activity();
        activity.setStatus(status);
        activity.setId(id);
        this.updateById(activity);
    }

    @Override
    public Page<Activity> page(ActivityPageDTO activityPageDTO) throws IOException {
        Page<Activity> pageInfo = new Page<>(activityPageDTO.getPage(), activityPageDTO.getLimit());
        // 构造条件
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        Page<Activity> activityPage = activityMapper.selectPage(pageInfo, queryWrapper);

//        //获取request
//        SearchRequest request = new SearchRequest("activity");
//        //DSL语句
//        Integer limit = activityPageDTO.getLimit();
//        Integer page = activityPageDTO.getPage();
//        request.source().from(page)
//                .size(limit)
//                .query(QueryBuilders.matchAllQuery())
//                .sort("createTime", SortOrder.ASC)
//                .highlighter(SearchSourceBuilder.highlight()
//                        .field("cover")
//                        .preTags("<em>")
//                        .postTags("</em>"));
//
//        SearchResponse search = client.search(request, RequestOptions.DEFAULT);
//
//        return handleResponse(search);
        return activityPage;
    }

    private Page<Activity> handleResponse(SearchResponse response) {
        SearchHits searchHits = response.getHits();
        // 1.获取总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到" + total + "条数据");
        // 2.遍历结果数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            // 3.得到_source，也就是原始json文档
            String source = hit.getSourceAsString();
            // 4.反序列化并打印
            JSON.parseObject(source, Activity.class);
        }
        return null;
    }
}
